
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "messagelog.h"
#include <qmessagebox.h>
#include "robotcontrol.h"
#include <QDebug>
#include "ethercatVendorId.h"
#include <QStandardItemModel>
#include "messagelog.h"
#include "powermanagerNew.h"
#include "robotbase.h"

#define D_ROBOT_ID 1
extern RobotControl* robCtl;
extern PowerManager* p_powerManger;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    ui->lineEdit->setFocus();
//    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
    messageLog =   MessageLog::getInstance();
    userInfo.account =ENUM_POWER_ADMIN;
    choose_axis =0;
    robotId = 1;
    messageSize=0;
    servoOnState=false;
    debugFlag=0;

    initialIoDisplay();
    timer=new QTimer();
    connect( timer, SIGNAL(timeout()), this, SLOT(timeOut_slot()) );
    timer->start( 500 );

    //initial
    model_ethercatSlavesInfo = new QStandardItemModel();
    QStringList labels3 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_ethercatSlavesInfo->setHorizontalHeaderLabels(labels3);
    ui->tableView_ethercatSlavesInfo->setColumnWidth(1,120);

    model_axisgroupCalibrateInfo = new QStandardItemModel();
    QStringList labels4 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_axisgroupCalibrateInfo->setHorizontalHeaderLabels(labels4);
    ui->tableView_axisgroupCalibrateInfo->setColumnWidth(1,120);

    model_ecatDebugInfo = new QStandardItemModel();
    QStringList labels5 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_axisgroupCalibrateInfo->setHorizontalHeaderLabels(labels5);
    ui->tableView_ethercatDebugInfo->setColumnWidth(1,120);

    model_canDebugInfo = new QStandardItemModel();
    model_modbusDebugInfo = new QStandardItemModel();


}

MainWindow::~MainWindow()
{
    if(NULL!=ui)
    {
        delete ui;
        ui = NULL;
    }
}

int MainWindow::showMotionForm()
{

}

int MainWindow::setDebugFlag(int debugFlagIn)
{
    debugFlag=debugFlagIn;
    return 1;
}

int MainWindow::setMainVersion(string versionIn, string dateIn)
{
    QString info="主版本:";
    info+=QString::fromStdString(versionIn)+" "+QString::fromStdString(dateIn);
    ui->mainVersionInfo->setText(info);
    return 1;
}


void MainWindow::showDebugInfo_ethercat(EthercatDebugInfo info)
{
    model_ecatDebugInfo->clear();
    //定义item
    QStandardItem* item = 0;

    item = new QStandardItem("MsCommandState");
    model_ecatDebugInfo->setItem(0,0,item);
    item = new QStandardItem(QString("%1").arg(info.masterCommandState));
    model_ecatDebugInfo->setItem(0,1,item);

    item = new QStandardItem("MsCurrentState");
    model_ecatDebugInfo->setItem(1,0,item);
    item = new QStandardItem(QString("%1").arg(info.masterCurrentState));
    model_ecatDebugInfo->setItem(1,1,item);

    item = new QStandardItem("MsOldState");
    model_ecatDebugInfo->setItem(2,0,item);
    item = new QStandardItem(QString("%1").arg(info.masteroldState));
    model_ecatDebugInfo->setItem(2,1,item);

    item = new QStandardItem("stage");
    model_ecatDebugInfo->setItem(3,0,item);
    item = new QStandardItem(QString("%1").arg(info.stage));
    model_ecatDebugInfo->setItem(3,1,item);

    item = new QStandardItem("wkc_lost_max");
    model_ecatDebugInfo->setItem(4,0,item);
    item = new QStandardItem(QString("%1").arg(info.wkc_lost_max));
    model_ecatDebugInfo->setItem(4,1,item);

    item = new QStandardItem("wkc_count");
    model_ecatDebugInfo->setItem(5,0,item);
    item = new QStandardItem(QString("%1").arg(info.wkc_count));
    model_ecatDebugInfo->setItem(5,1,item);

    item = new QStandardItem("wkc_state");
    model_ecatDebugInfo->setItem(6,0,item);
    item = new QStandardItem(QString("%1").arg(info.wkc_state));
    model_ecatDebugInfo->setItem(6,1,item);

    item = new QStandardItem("opLoopCount");
    model_ecatDebugInfo->setItem(7,0,item);
    item = new QStandardItem(QString::number(info.opLoopCount,'f',1));
    model_ecatDebugInfo->setItem(7,1,item);


    item = new QStandardItem("cycle_us");
    model_ecatDebugInfo->setItem(8,0,item);
    item = new QStandardItem(QString("%1").arg(info.cycle_us));
    model_ecatDebugInfo->setItem(8,1,item);

    item = new QStandardItem("dcTimerShift");
    model_ecatDebugInfo->setItem(9,0,item);
    item = new QStandardItem(QString("%1").arg(info.dcTimerShift));
    model_ecatDebugInfo->setItem(9,1,item);

    item = new QStandardItem("refTime");
    model_ecatDebugInfo->setItem(10,0,item);
    item = new QStandardItem(QString("%1").arg(info.refTime));
    model_ecatDebugInfo->setItem(10,1,item);

    item = new QStandardItem("dc_diff_ns");
    model_ecatDebugInfo->setItem(11,0,item);
    item = new QStandardItem(QString("%1").arg(info.dc_diff_ns));
    model_ecatDebugInfo->setItem(11,1,item);

    item = new QStandardItem("pcSumTime");
    model_ecatDebugInfo->setItem(12,0,item);
    item = new QStandardItem(QString("%1").arg(info.pcSumTime));
    model_ecatDebugInfo->setItem(12,1,item);

    item = new QStandardItem("pcCycleCount");
    model_ecatDebugInfo->setItem(13,0,item);
    item = new QStandardItem(QString("%1").arg(info.pcCycleCount));
    model_ecatDebugInfo->setItem(13,1,item);

    item = new QStandardItem("ecatPeriodTimerMonitor_now");
    model_ecatDebugInfo->setItem(14,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatPeriodTimerMonitor_now/1000));
    model_ecatDebugInfo->setItem(14,1,item);

    item = new QStandardItem("ecatPeriodTimerMonitor_max");
    model_ecatDebugInfo->setItem(15,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatPeriodTimerMonitor_max/1000));
    model_ecatDebugInfo->setItem(15,1,item);

    item = new QStandardItem("ecatPeriodTimerMonitor_min");
    model_ecatDebugInfo->setItem(16,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatPeriodTimerMonitor_min/1000));
    model_ecatDebugInfo->setItem(16,1,item);

    item = new QStandardItem("ecatReceiveMonitor_now");
    model_ecatDebugInfo->setItem(17,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatReceiveMonitor_now/1000));
    model_ecatDebugInfo->setItem(17,1,item);

    item = new QStandardItem("ecatReceiveMonitor_max");
    model_ecatDebugInfo->setItem(18,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatReceiveMonitor_max/1000));
    model_ecatDebugInfo->setItem(18,1,item);

    item = new QStandardItem("ecatReceiveMonitor_min");
    model_ecatDebugInfo->setItem(19,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatReceiveMonitor_min/1000));
    model_ecatDebugInfo->setItem(19,1,item);

    item = new QStandardItem("ecatSendMonitor_now");
    model_ecatDebugInfo->setItem(20,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatSendMonitor_now/1000));
    model_ecatDebugInfo->setItem(20,1,item);

    item = new QStandardItem("ecatSendMonitor_max");
    model_ecatDebugInfo->setItem(21,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatSendMonitor_max/1000));
    model_ecatDebugInfo->setItem(21,1,item);

    item = new QStandardItem("ecatSendMonitor_min");
    model_ecatDebugInfo->setItem(22,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatSendMonitor_min/1000));
    model_ecatDebugInfo->setItem(22,1,item);

    item = new QStandardItem("ecatCustomMonitor_now");
    model_ecatDebugInfo->setItem(23,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatCustomMonitor_now/1000));
    model_ecatDebugInfo->setItem(23,1,item);

    item = new QStandardItem("ecatCustomMonitor_max");
    model_ecatDebugInfo->setItem(24,0,item);
    item = new QStandardItem(QString("%1").arg(info.ecatCustomMonitor_max/1000));
    model_ecatDebugInfo->setItem(24,1,item);

    item = new QStandardItem("ecatCustomMonitor_min");
    model_ecatDebugInfo->setItem(25,0,item);
    item = new QStandardItem(QString::number(info.ecatCustomMonitor_min/1000));
    model_ecatDebugInfo->setItem(25,1,item);


    for(int i=0;i<info.slaves_al.size();i++)
    {
        item = new QStandardItem("slave_al");
        model_ecatDebugInfo->setItem(i+26,0,item);
        item = new QStandardItem(QString("%1").arg(info.slaves_al[i]));
        model_ecatDebugInfo->setItem(i+26,1,item);
    }


    ui->tableView_ethercatDebugInfo->setModel(model_ecatDebugInfo);
    ui->tableView_ethercatDebugInfo->resizeColumnToContents(0);
    ui->tableView_ethercatDebugInfo->resizeColumnToContents(1);
    ui->tableView_ethercatDebugInfo->resizeColumnToContents(2);
    ui->tableView_ethercatDebugInfo->show();
}


void MainWindow::showMessageInfo()
{
    //显示消息
    std::vector<struct Message> messageVector;
//    p_powerManger->updateControllerMessage(   userInfo, messageVector );
    messageLog-> updateControllerInfoLevel2(  messageVector );
    QString messageText;
    if(messageSize!=messageVector.size())
    {
        ui->listWidget_2->clear();
        messageText.clear();
        for(int i=0;i<messageVector.size();i++)
        {
            messageText=QString::number(messageVector[i].messageCode)
                    +QString::fromStdString(messageVector[i].MessageInformation);

            ui->listWidget_2->addItem(messageText);

        }
        messageSize=messageVector.size();

    }
}

void MainWindow::showMotorStatusInfo()
{
    QVector<ServoState> pData = robCtl->getServoDataRobot( robotId );

    unsigned short tmpStatusOut;
    unsigned short tmpErrorCodeOut=0;
    unsigned int vendorIdOut;
    if(pData.size()){
        int count=-1;
        if( ++count < pData.size()){
            robCtl->getMotorVendorId(robotId,count,vendorIdOut);
            robCtl->getMotorStatusWord(robotId,count,tmpStatusOut);

                ui->label_err_0->setText( QString::number( pData.at( count ).errorCode) );

            ui->label_pos_0->setText( QString::number( pData.at( count ).position) );

            ui->label_14->setText( QString::number( tmpStatusOut ));
        }
        if( ++count < pData.size()){
            robCtl->getMotorVendorId(robotId,count,vendorIdOut);
            robCtl->getMotorStatusWord(robotId,count,tmpStatusOut);

                ui->label_err_1->setText( QString::number( pData.at( count ).errorCode) );

            ui->label_pos_1->setText( QString::number( pData.at( count ).position) );

                  ui->label_15->setText( QString::number( tmpStatusOut) );
        }
        if( ++count < pData.size()){
            robCtl->getMotorVendorId(robotId,count,vendorIdOut);
            robCtl->getMotorStatusWord(robotId,count,tmpStatusOut);

                ui->label_err_2->setText( QString::number( pData.at( count ).errorCode) );

            ui->label_pos_2->setText( QString::number( pData.at( count ).position) );

                ui->label_16->setText( QString::number( tmpStatusOut) );
        }
        if( ++count < pData.size()){
            robCtl->getMotorVendorId(robotId,count,vendorIdOut);
            robCtl->getMotorStatusWord(robotId,count,tmpStatusOut);

                ui->label_err_3->setText( QString::number( pData.at( count ).errorCode) );

            ui->label_pos_3->setText( QString::number( pData.at( count ).position) );

                ui->label_17->setText( QString::number( tmpStatusOut) );
        }
        if( ++count < pData.size()){
            robCtl->getMotorVendorId(robotId,count,vendorIdOut);
            robCtl->getMotorStatusWord(robotId,count,tmpStatusOut);

                ui->label_err_4->setText( QString::number( pData.at( count ).errorCode) );

            ui->label_pos_4->setText( QString::number( pData.at( count ).position) );

                    ui->label_18->setText( QString::number( tmpStatusOut) );
        }
        if( ++count < pData.size()){
            robCtl->getMotorVendorId(robotId,count,vendorIdOut);
            robCtl->getMotorStatusWord(robotId,count,tmpStatusOut);

                ui->label_err_5->setText( QString::number( pData.at( count ).errorCode) );

            ui->label_pos_5->setText( QString::number( pData.at( count ).position) );

                ui->label_19->setText( QString::number( tmpStatusOut) );
        }
    }



    //显示servoOn状态



        if(true==robCtl->isServoOnRobot(robotId))
        {
            ui->radioButton->setChecked(true);
        }
        else
        {
            ui->radioButton->setChecked(false);
        }


}


void MainWindow::timeOut_slot()
{
    if(1==debugFlag)
    {
        qDebug()<<"MainWindow::timeOut_slot() In";
    }

    //显示调试信息
    showDebugInfo();

    showMotorStatusInfo();

    showMessageInfo();

    updateIoStatus();


}

void MainWindow::setDoValue(bool checked)
{
    qDebug()<<"testForm::setDoValue";
    RobotBase*rob=robCtl->robotsMap.value( D_ROBOT_ID,NULL );
    if(rob==NULL)
        return ;

    QCheckBox* box = qobject_cast<QCheckBox *>(sender());
    if( box )
    {
        int index =  box->text().toInt();
        QString str= ((QGroupBox*)box->parent())->title();
        if( str=="DO" )
        {
            //BUG,物理ＩＯ获取的ＤＯ比控件状态延迟的话，会错误执行下面的设置。
            rob->logicRobotIo->setDo( index, box->isChecked() );
            qDebug()<<"testForm DO index    :"  << index << checked <<  rob->logicRobotIo->ioDi  << rob->logicRobotIo ->ioDo ;
        }
    }
}

void MainWindow::updateIoStatus()
{
    RobotBase*rob=robCtl->robotsMap.value( D_ROBOT_ID,NULL );
    for(int j=0; j< rob->logicRobotIo->diCount ; j++)
    {
        if(j<32)
        {
        QCheckBox* box= (QCheckBox*)ui->groupBox_di->children().at(j);
        box->setChecked(   rob->logicRobotIo->ioDi[j]  );
        }
    }
    for(int j=0; j< rob->logicRobotIo->aiCount ; j++)
    {
        if(j<32)
        {
        ai_box[j]->setText( QString::number(  (double)(rob->logicRobotIo->ioAi[j]) ));
        }
    }

//        if( pos_count_2<rob->IO->doCount )
//        {
//            pos_count_2++;
//            if( rob->IO->ioDo[pos_count_2] )
//                rob->IO->ioDo[pos_count_2]= 0;
//            else
//                rob->IO->ioDo[pos_count_2]= 1;
//        }else{
//            pos_count_2=0;
//        }

    for(int j=0; j< rob->logicRobotIo->doCount ; j++)
    {
        if(j<32)
        {
        QCheckBox* box= (QCheckBox*)ui->groupBox_do->children().at(j);
        box->setChecked(   rob->logicRobotIo->ioDo[j]  );
        }
    }

    for(int j=0; j< rob->logicRobotIo->aoCount ; j++)
    {
        if(j<32)
        {
        float val =   ao_box[j]->text().toFloat();
//            if( val<=2.0  && val>=0 )
            rob->logicRobotIo->ioAo[j] = val;
        }
    }
}

void MainWindow::initialIoDisplay()
{
    if(1)
    {

        for(int j=0;j< 32 ; j++)
        {
            di_box[j]=new QCheckBox(ui->groupBox_di);
            di_box[j]->setGeometry(5, 20+22*j,50, 20);
            di_box[j]->setText( QString::number(j) );
        }

        for(int j=0; j<32; j++)
        {
            do_box[j]=new QCheckBox(ui->groupBox_do);
            do_box[j]->setGeometry(5,20+22*j,50,20);
            do_box[j]->setText( QString::number(j) );
            //不能使用　toggled，状态变化，而不是被点击也会触发，导致ＢＵＧ。
//            connect(  do_box[j], SIGNAL(toggled(bool)), this, SLOT(setDoDiState(bool)));
            connect(  do_box[j], SIGNAL(clicked(bool)), this, SLOT(setDoValue(bool)));
        }

        for(int j=0; j< 32 ; j++)
        {
            ai_box[j] =new QLineEdit(ui->groupBox_ai);
            ai_box[j]->setGeometry(5, 20+22*j, 50, 20);
            ai_box[j]->setText( QString::number(j) );
        }

        for(int j=0; j< 32 ; j++)
        {
            ao_box[j]=new QLineEdit(ui->groupBox_ao);
            ao_box[j]->setGeometry(5, 20+22*j,50,20);
            ao_box[j]->setText( QString::number(j) );
        }

//        for(int j=0; j< 12 ; j++)
//        {
//            axis_box[j]=new QLabel(ui->tab);
//            axis_box[j]->setGeometry(ui->label_master_state->x(), ui->label_master_state->y()+30+20*j,ui->label_master_state->width(),20);
//            axis_box[j]->setText( "axis" );
//        }
    }
}


void MainWindow::showDebugInfo()
{

    if(true==ui->checkBox_ecatInfoUpdateEnable->isChecked())
    {
        EthercatDebugInfo ecatDebugInfo;
        robCtl->getEthercatDebugInfo( ecatDebugInfo);
        showDebugInfo_ethercat(ecatDebugInfo);
    }



}



void MainWindow::on_pushButton_quick_stop_clicked()
{

    robCtl->quickStopRobot( -1 );
}

void MainWindow::on_spinBox_time_valueChanged(int arg1)
{
    timer->setInterval( arg1 );
}

void MainWindow::on_checkBox_on_off_toggled(bool checked)
{
    if( checked )
    {
        qDebug()<< "servoOnrobot(  ) : "<<  robotId  ;
        robCtl->servoOnRobot(robotId);
//       p_powerManger->robotManager->robotsMap.value(robotId)->threadMotionEngine->mcControllerInterpreter->servoOn();
       servoOnState=1;
    }
    else
    {
        robCtl->servoOffRobot(robotId);
       //p_powerManger->robotManager->robotsMap.value(robotId)->threadMotionEngine->mcControllerInterpreter->servoOff();
        servoOnState=0;


    }
}



void MainWindow::on_pushButton_clear_clicked()
{
    robCtl->resetErrorRobot( -1 );

    if( messageLog )
    {
        messageLog->clearAllMessage();
    }

}


void MainWindow::on_checkBox_toggled(bool checked)
{
    if ( checked ){
        timer->start(500);
    }else{
        timer->stop();
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    vrepForm->setGeometry(800,20,300,300);
    vrepForm->show();
}

void MainWindow::on_pushButton_4_clicked()
{
    ecatForm->setGeometry(700,30,300,300);
    ecatForm->show();
}

void MainWindow::on_pushButton_5_clicked()
{
//    motionForm->setGeometry(750,40,500,500);
//    motionForm->show();
//    p_powerManger->showMotionForm(robotId);
}



void MainWindow::on_pushButton_read_sdo_clicked()
{
    int sdoMethod=ui->comboBox_sdoMethod->currentIndex();
    bool isOk;
    int rObIndex=ui->lineEdit_mainIndex->text().toInt(&isOk,16);
    int rObSubIndex=ui->lineEdit_subIndex->text().toInt(&isOk,16);
    LONG64 rData=-1;
    int rDataLen=4;
    int motorIndex=ui->lineEdit_motorIndex->text().toInt();
    if(0==sdoMethod)
    {
            rDataLen=robCtl->getSdo(robotId,motorIndex,rObIndex,rObSubIndex,rData,rDataLen);
    }
    else if(1==sdoMethod)
    {
            rDataLen=robCtl->getSdoByNodeId(motorIndex,rObIndex,rObSubIndex,rData,rDataLen);
    }
    else if(2==sdoMethod)
    {
            rDataLen=robCtl->getSdoByAlias(motorIndex,rObIndex,rObSubIndex,rData,rDataLen);
    }

    if(1==rDataLen)
    {
        ui->lineEdit_length->setText(QString::number(rDataLen));
        QString tmp;
        tmp.clear();
        tmp.sprintf("%02x",rData);
        ui->lineEdit_value->setText(tmp);
       QMessageBox::information(NULL, "read sdo success成功!", "成功", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "read sdo failed 失败!", "失败", QMessageBox::Yes );
    }

}

void MainWindow::on_pushButton_write_sdo_clicked()
{
    int sdoMethod=ui->comboBox_sdoMethod->currentIndex();
    bool isOk;
    int rObIndex=ui->lineEdit_mainIndex->text().toInt(&isOk,16);
    int rObSubIndex=ui->lineEdit_subIndex->text().toInt(&isOk,16);
    LONG64 rData=ui->lineEdit_value->text().toLongLong(&isOk,16);
    int rDataLen=ui->lineEdit_length->text().toInt();
    int motorIndex=ui->lineEdit_motorIndex->text().toInt();
    int returnKey;
    int isSucess;
    if(0==sdoMethod)
    {
        isSucess=robCtl->setSdo(robotId,motorIndex
                                       ,rObIndex,rObSubIndex,rData,rDataLen);
    }
    else if(1==sdoMethod)
    {
        isSucess=robCtl->setSdoByNodeId(motorIndex
                                       ,rObIndex,rObSubIndex,rData,rDataLen);
    }
    else if(2==sdoMethod)
    {
        isSucess=robCtl->setSdoByAlias(motorIndex
                                       ,rObIndex,rObSubIndex,rData,rDataLen);
    }
    if(1==isSucess)
    {
       QMessageBox::information(NULL, "write sdo success成功!", "成功", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "writesdo failed 失败!", "失败", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_clearBatteryError_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否清除电池错误！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }

    bool tmpReturn;
    int axisId=ui->lineEdit_motorIndex_torque->text().toInt();
    if(axisId<0)
    {
        tmpReturn=robCtl->clearMotorBatteryError(robotId);
    }
    else
    {
        tmpReturn=robCtl->clearMotorBatteryErrorByAxis(robotId,axisId);
    }

    if(true==tmpReturn)
    {
       QMessageBox::information(NULL, "清除机器人电机电池错误成功!", "成功", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "清除机器人电机电池错误失败!", "失败", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_setCurrentRobotDcMode_clicked()
{
    if(true==robCtl->setEthercatMotorDcUserMode(robotId))
    {
       QMessageBox::information(NULL, "DC模式设置成功!", "成功", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "DC模式设置失败!", "失败", QMessageBox::Yes );
    }
}


void MainWindow::on_pushButton_writeAlias_clicked()
{
    int nodeId =ui->lineEdit_nodeId->text().toInt();
    unsigned short alias=ui->lineEdit_alias->text().toInt();
    int returnFlag;
    robCtl->setAlias(nodeId,alias);
    QMessageBox::information(NULL, "setAlias", "setAlias success", QMessageBox::Yes );
}

void MainWindow::on_pushButton_getSlavesInfo_clicked()
{
    std::vector<EcatBusDevInfo_gsoap> slavesInfo;
    robCtl->getEthercatSlavesInformation(slavesInfo);

    model_ethercatSlavesInfo->clear();
    //定义item
    QStandardItem* item = 0;
    int lineSize=14;

    for(int i=0;i<slavesInfo.size();i++)
    {
        item = new QStandardItem("node id");
        model_ethercatSlavesInfo->setItem(lineSize*i+0,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].node));
        model_ethercatSlavesInfo->setItem(lineSize*i+0,1,item);

        item = new QStandardItem("alias");
        model_ethercatSlavesInfo->setItem(lineSize*i+1,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].alias));
        model_ethercatSlavesInfo->setItem(lineSize*i+1,1,item);

        item = new QStandardItem("vendorId");
        model_ethercatSlavesInfo->setItem(lineSize*i+2,0,item);
        //8代表宽度，16表示16进制，空位补零
        item = new QStandardItem(QString("0x%1").arg(slavesInfo[i].vendorId, 8, 16, QLatin1Char('0')));
        model_ethercatSlavesInfo->setItem(lineSize*i+2,1,item);


        item = new QStandardItem("productCode");
        model_ethercatSlavesInfo->setItem(lineSize*i+3,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].productCode));
        model_ethercatSlavesInfo->setItem(lineSize*i+3,1,item);

        item = new QStandardItem("revisionNumber");
        model_ethercatSlavesInfo->setItem(lineSize*i+4,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].revisionNumber));
        model_ethercatSlavesInfo->setItem(lineSize*i+4,1,item);

        item = new QStandardItem("serial_number");
        model_ethercatSlavesInfo->setItem(lineSize*i+5,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].serial_number));
        model_ethercatSlavesInfo->setItem(lineSize*i+5,1,item);

        item = new QStandardItem("al_state");
        model_ethercatSlavesInfo->setItem(lineSize*i+6,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].al_state));
        model_ethercatSlavesInfo->setItem(lineSize*i+6,1,item);

        item = new QStandardItem("error_flag");
        model_ethercatSlavesInfo->setItem(lineSize*i+7,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].error_flag));
        model_ethercatSlavesInfo->setItem(lineSize*i+7,1,item);

        item = new QStandardItem("sync_count");
        model_ethercatSlavesInfo->setItem(lineSize*i+8,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].sync_count));
        model_ethercatSlavesInfo->setItem(lineSize*i+8,1,item);

        item = new QStandardItem("sdo_count");
        model_ethercatSlavesInfo->setItem(lineSize*i+9,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].sdo_count));
        model_ethercatSlavesInfo->setItem(lineSize*i+9,1,item);

        item = new QStandardItem("devType");
        model_ethercatSlavesInfo->setItem(lineSize*i+10,0,item);
        item = new QStandardItem(QString("%1").arg(slavesInfo[i].devType));
        model_ethercatSlavesInfo->setItem(lineSize*i+10,1,item);

        item = new QStandardItem("devName");
        model_ethercatSlavesInfo->setItem(lineSize*i+11,0,item);
        item = new QStandardItem(QString::fromStdString(slavesInfo[i].devName));
        model_ethercatSlavesInfo->setItem(lineSize*i+11,1,item);

        item = new QStandardItem("mappingObjectFile");
        model_ethercatSlavesInfo->setItem(lineSize*i+12,0,item);
        item = new QStandardItem(QString::fromStdString(slavesInfo[i].mappingObjectFile));
        model_ethercatSlavesInfo->setItem(lineSize*i+12,1,item);
    }


    ui->tableView_ethercatSlavesInfo->setModel(model_ethercatSlavesInfo);
    ui->tableView_ethercatSlavesInfo->resizeColumnToContents(0);
    ui->tableView_ethercatSlavesInfo->resizeColumnToContents(1);
    ui->tableView_ethercatSlavesInfo->resizeColumnToContents(2);
    ui->tableView_ethercatSlavesInfo->show();
    QMessageBox::information(NULL, "getSlavesInfo", "getSlavesInfo success", QMessageBox::Yes );
}


void MainWindow::on_pushButton_read_sdo_2_clicked()
{
    //set torque
    robCtl->setMotorTorqueLimit(robotId,ui->lineEdit_motorIndex_torque->text().toInt(),
                               ui->lineEdit_maxTorquePos->text().toInt(),
                               ui->lineEdit_maxTorqueNeg->text().toInt());
    QMessageBox::information(NULL, "setMaxTorque", "setMaxTorque success", QMessageBox::Yes );

}

void MainWindow::on_pushButton_setTorqueFilter_clicked()
{
    //set torque filter
    robCtl->setMotorTorqueFilterTime(robotId,ui->lineEdit_motorIndex_torque->text().toInt(),
                               ui->lineEdit_torqueFilter->text().toInt());
    QMessageBox::information(NULL, "setMaxTorque", "setMaxTorque success", QMessageBox::Yes );
}


void MainWindow::on_checkBox_positionIncrementValid_clicked(bool checked)
{
    if(checked)
    {
        p_powerManger->setValidAxisPositionIncre(ui->comboBox_axis->currentIndex(),
                                                 ui->lineEdit_positionCommandIncrement->text().toInt());
    }
    else
    {
        p_powerManger->setValidAxisPositionIncre(-1,0);
    }

}




